<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ include file="/WEB-INF/jsps/tag.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户管理</title>
<%@ include file="/WEB-INF/jsps/common_css.jsp"%>
<%@ include file="/WEB-INF/jsps/common_js.jsp"%>

<script type="text/javascript">

$(function(){
	
	$('#user_list_id').datagrid({    
	    url:'${baseurl}user/list',
	    toolbar:"#user_toolbar_id"
	});
	
	
	//新增
	$("#user_add_id").click(function(){
		$("#user_edit_id").dialog("open");
	});
	
	//取消
	$("#user_edit_dialog_buttons_cancel_id").click(function(){
		$("#user_edit_id").dialog("close");
		$('#user_form_id').form("clear");
	});
	
// 	//提交表单
	$("#user_edit_dialog_buttons_ok_id").click(function(){
		if(!$('#user_form_id').form("validate")){
			return;
		}
		
		var id = $("#id").val();
		var username = $("#user_username_id").val();
		var realname = $("#user_realname_id").val();
		var departmentId = $("#user_departmentId").combobox("getValue");
		var roleIds = $("#user_roleIds").combobox("getValues");
		
// 		alert("id===>"+id+"   username===>"+username+" realname===>"+realname+
// 				 	" departmentId==>"+departmentId+"  roleIds==>"+roleIds);	
		
		$.post("${baseurl}user/saveOrUpdate", 
				{ "id":id,"username":username,"realname":realname,"departmentId":departmentId,"roleIds":roleIds.join(",")},
			 function(data){
			  if(data.success){
		        	$('#user_list_id').datagrid("reload");
		        	$('#user_form_id').form("clear");
		        	$.messager.show({title:'系统提示',msg:data.content,timeout:5000,showType:'slide'});
		        }
			    $("#user_edit_id").dialog("close");
			    $.messager.show({title:'操作失败',msg:data.content,timeout:5000,showType:'slide'});
			  }
			 ,"json"); 
	});
	
	
	//删除
	$("#user_delete_id").click(function(){
		var selected =  $("#user_list_id").datagrid("getSelected");
		if(selected == null){
			alert("请选中你要删除的记录.");
		}else{
			$.post("${baseurl}user/delete", { "id":selected.id},
					   function(data){
					    	if(data.success){
					    		$('#user_list_id').datagrid("reload");
					    	}else{
					    		$('#user_form_id').form("clear");
					    	}
					    	$.messager.show({title:'系统提示',msg:data.content,timeout:5000,showType:'slide'});
					   }, "json");
		}
	});
	
	//修改
	$("#user_update_id").click(function(){
		var selected =  $("#user_list_id").datagrid("getSelected");
		if(selected == null){
			alert("请选中你要修改的记录.");
		}else{
			$('#user_form_id').form("load","${baseurl}user/get?id="+selected.id);
			$("#id").val(selected.id);
			//选中部门
			$("#user_departmentId").combobox("setValue",selected.departmentId);
			//选中角色
			$("#user_roleIds").combobox("setValues",selected.roleIds);
			$("#user_edit_id").dialog("open");
		}
	});
	
	
	
});
	
	//解锁/锁定用户
	function lock(id){
		$.post("${baseurl}user/lock", { "id":id},
				   function(data){
				    	if(data.success){
				    		$('#user_list_id').datagrid("reload");
				    	}
				    	$.messager.show({title:'系统提示',msg:data.content,timeout:5000,showType:'slide'});
				   }, "json");
	}

	

</script>
</head>
<body>
	<table id="user_list_id" style="width:100%;height:100%;"
		data-options="fitColumns:true,singleSelect:true,pagination:true,pagePosition:'bottom'">   
	    <thead>   
	        <tr>   
	            <th data-options="field:'id',width:20">id</th>   
	            <th data-options="field:'username',width:20">登陆名</th>
	            <th data-options="field:'realname',width:20">姓名</th>
	            <th data-options="field:'department',width:20,formatter:function(value,row,index){
	            		if(value=='' || value == null){
	            			return '开发公司';
	            		}else{
	            			return value;
	            		}
	           		}">所属部门</th>
	            <th data-options="field:'roles',width:20">拥有角色</th>
              	<th data-options="field:'lastLoginDate',width:20,formatter:function(value,row,index){
            			return dateFormatter_yyyyMMddHHmmss(value);
            		}">上次登录时间</th>
            		   
            	<th data-options="field:'locked',width:20,formatter:function(value,row,index){
            			if(value) return '<span style=color:red onclick=lock('+row.id+')>是</span>';
            				return '<span onclick=lock('+row.id+')>否</span>';
            		}">是否锁定</th>   
	        </tr>   
	    </thead>   
	</table>
	
	<!-- 数据表格toolbar -->
	<div id="user_toolbar_id">
		<a id="user_add_id" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">新增 </a>
		<a id="user_update_id" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a>
		<a id="user_delete_id" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除</a>
	</div>
	
	<!-- 编辑对话框 -->
	<div id="user_edit_id" class="easyui-dialog" title="用户管理" style="width:500px;height:220px"   
        data-options="iconCls:'icon-save',modal:true,closed:true,buttons:'#user_edit_dialog_buttons_id'">   
   		<form id="user_form_id" method="post">
   			<input id="id" type="hidden" name="id"/>
   			<table>
   				<tr>
   					<td>登陆名 </td>
   					<td>
					     <input id="user_username_id" class="easyui-validatebox" type="text" name="username" data-options="required:true" />
   					</td>
   				</tr>
   				<tr>
   					<td>姓名:</td>
   					<td>
				        <input id="user_realname_id" class="easyui-validatebox" type="text" name="realname" data-options="required:true" />
   					</td>
   				</tr>
   				<tr>
   					<td>部门</td>
   					<td>
				       <input id="user_departmentId" class="easyui-combobox" name="departmentId"   
    					  data-options="valueField:'id',textField:'name',url:'${baseurl}department/all',editable:false,required:true" />  
   					</td>
   				</tr>
   				<tr>
   					<td>角色</td>
   					<td>
				        <input id="user_roleIds" class="easyui-combobox"  name="roleIds" data-options="required:true,valueField:'id',
				        	textField:'name',url:'${baseurl}role/all',editable:false,multiple:true" style="width:400px"> 
   					</td>
   				</tr>
   			</table>
		</form>
	</div>
	
	<!-- 编辑对话框的buttons -->
	<div id="user_edit_dialog_buttons_id">
		<a id="user_edit_dialog_buttons_ok_id" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">保存</a>
		<a id="user_edit_dialog_buttons_cancel_id" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">关闭</a>
	</div>
	
</body>
</html>